<?php
namespace bee;
ini_set("error_reporting", ini_get("error_reporting") ^ E_DEPRECATED);

//global $sys_working_path;
set_include_path(get_include_path() . PATH_SEPARATOR . config::getInstance()->sys_BEE_PATH . "/lib/ext");
//echo get_include_path();
require_once "PHPExcel.php";
require_once "PHPExcel/IOFactory.php";

class xrClass extends object
{	public $activeRow;
	private $template;
	/**
	 * @var PHPExcel $Excel;
	 */
	protected $Excel;
	protected $parent;
/**
 * @todo dodati formu na kojoj se nalazi report kao parent property reporta, kako bi report mogao vući podatke sa forme
 */
/**
 * 
 * @param $template
 * @return unknown_type
 */
	public function __construct($template = "", form $parent = NULL)
	{	$Reader = \PHPExcel_IOFactory::createReader("Excel2007");
		if($template)
		{	$this->Excel = $Reader->load($template);
			$this->template = $template;
		}	else
		{	$this->Excel = new PHPExcel();
		}
		$this->activeRow = 2;
		$this->parent = $parent;
	}

	public function addRow(\DOMNode $xmlNode)
	{	$cn = $xmlNode->childNodes;
		$actCol = 1;
		$this->Excel->getActiveSheet()->setCellValueByColumnAndRow($actCol++, $this->activeRow, $this->activeRow - 2);
		foreach ($cn as $cNode)
		{	$this->Excel->getActiveSheet()->setCellValueByColumnAndRow($actCol++, $this->activeRow, $cNode->nodeValue);
		}
		$this->activeRow++;
	}

	public function addFinalSum(\DOMNode $xmlNode = null)
	{	$cn = $xmlNode->childNodes;
		$actCol = "A";
		$this->Excel->getActiveSheet()->setCellValueByColumnAndRow($actCol, $this->activeRow, "UKUPNO:");
		$this->Excel->getActiveSheet()->getStyleByColumnAndRow($actCol, $this->activeRow)->getFont()->setBold(true);
		$actCol++;
		for($i = 1; $i < $cn->length; $i++)
		{	$pActRow = $this->activeRow - 1;
			$sum = "=SUM(" .$actCol . "1:" . $actCol . $pActRow . ")";
//			echo "/$sum";
//echo " ::" . $cn->item($i)->nodeValue . ":: ";
			$style = $this->Excel->getActiveSheet()->getStyleByColumnAndRow($i, $this->activeRow);

			$style->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1);
			$style->getFont()->setBold(true);
			$this->Excel->getActiveSheet()->setCellValueByColumnAndRow($i, $this->activeRow, $sum);
			$actCol++;
		}
		$this->activeRow++;
	}


	public function publish($name = "", $format = "2003")
	{	if(!$name)
		{	$name = $this->template;
		}
		if($format == "2007")
		{	header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
			header('Content-Disposition: attachment;filename="' . $name . '.xlsx"');
			header("Cache-Control: max-age=0");
			$Writer = PHPExcel_IOFactory::createWriter($this->Excel, "Excel2007");
			$Writer->save("php://output");
		}	else
		{	header("Content-Type: application/vnd.ms-excel");
			header('Content-Disposition: attachment;filename="' . $name . 'myfile.xls"');
			header("Cache-Control: max-age=0");
			$Writer = PHPExcel_IOFactory::createWriter($this->Excel, "Excel5");
			$Writer->save("php://output");
		}
	}
	public function save($name, $format = "2003")
	{	if($format == "2007")
		{	if(!stristr(".xlsx", $name))
			{	$name .= ".xlsx";
			}
			$Writer = \PHPExcel_IOFactory::createWriter($this->Excel, "Excel2007");
		}	else
		{	if(!stristr(".xls", $name))
			{	$name .= ".xls";
			}
			$Writer = \PHPExcel_IOFactory::createWriter($this->Excel, "Excel5");

		}
		$Writer->save($name);
	}

}

?>